package org.procat.porrapp;

import java.util.Map;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;

public class PorraMainActivity extends Activity implements TextWatcher {
	String codiPorra;
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        codiPorra = getIntent().getStringExtra("porra");  
        
        Toast.makeText(getApplicationContext(), "Porra " + codiPorra, Toast.LENGTH_LONG).show();

        
        if (codiPorra.equals("nova")) 
        {
        	SharedPreferences prefs = getSharedPreferences("PorraData", 0);
        	int i=1;
        	for (;prefs.contains("Porra" + i) ;i++);        	
        	codiPorra = Integer.toString(i);
        	
            Toast.makeText(getApplicationContext(), "Nova porra amb número " + codiPorra, Toast.LENGTH_LONG).show();

        	
        	Editor edit = prefs.edit();
        	edit.putString("Porra" + codiPorra, "(no definit)");
        	edit.commit();
        }
        
        actualitzaLlista();
        Button btnNovaAposta = (Button)findViewById(R.id.btnNovaAposta);
        btnNovaAposta.setOnClickListener(new View.OnClickListener() 
        {
	        public void onClick(View v)
	        {
	        	novaAposta((Button) v);
	        }
        });
        
        Button btnEnviar = (Button)findViewById(R.id.btnEnviarApostants);
        btnEnviar.setOnClickListener(new View.OnClickListener() 
        {
	        public void onClick(View v)
	        {
	        	
	        	actualitzaLlista();
	        }
        });
        
        Button btnPublicar = (Button)findViewById(R.id.btnPublicar);
        btnPublicar.setOnClickListener(new View.OnClickListener() 
        {
	        public void onClick(View v)
	        {
	        	
	        	publicarAposta();
	        }
        });
        
    	EditText etxtAposta = (EditText)findViewById(R.id.txtAposta);
    	etxtAposta.addTextChangedListener(this);
    	

    }
    
    protected void publicarAposta() {
		
    	Intent shareIntent = new Intent(android.content.Intent.ACTION_SEND);
    	shareIntent.setType("text/plain");
    	
    	EditText etxtAposta = (EditText)findViewById(R.id.txtAposta);
    	
    	String bodyMsg= "Les següents apostes pel partit " + etxtAposta.getText() + " han estat realitzades:\n";
    	SharedPreferences prefs = getSharedPreferences("PorraData", 0);
    	Map<String, ?> mapaLlista = prefs.getAll();
    	
    	for(Map.Entry<String, ?> entry : mapaLlista.entrySet())
    	{
    		bodyMsg += " -"+ entry.getKey()+": "+ entry.getValue() + "\n";
    	}
    	
    	
    	shareIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "Porra del " + etxtAposta.getText());
    	shareIntent.putExtra(android.content.Intent.EXTRA_TEXT, bodyMsg);

    	startActivity(Intent.createChooser(shareIntent, "Publica a..."));
		
	}

	public void novaAposta(Button btn)
    {
    	Intent i = new Intent(PorraMainActivity.this, EditarApostaActivity.class);
    	i.putExtra("porra", codiPorra);
    	startActivity(i);
    }
    
    public void actualitzaLlista()
    { 
    	ListView llista = (ListView) findViewById(R.id.listView1);
    	ArrayAdapter<String> llistaAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1);
    	
    	if (codiPorra != "nova")
    	{
    	
    		SharedPreferences prefs = getSharedPreferences("PorraData", 0);
    		Map<String, ?> mapaLlista = prefs.getAll();
    		
    		EditText etxtAposta = (EditText) findViewById(R.id.txtAposta);
    		etxtAposta.setText(prefs.getString("Porra" + codiPorra, "(sense nom)"));
	    	
	    	for(Map.Entry<String, ?> entry : mapaLlista.entrySet())
	    	{
	    		if (entry.getKey().startsWith("Porra" + codiPorra + ".") && entry.getKey().endsWith(".aposta"))
	    			llistaAdapter.add( prefs.getString(entry.getKey() + "nt","(no definit)") + " ==> "+ entry.getValue());
	    	}
    	}    	
    	
    	llista.setAdapter(llistaAdapter);
    }

	public void afterTextChanged(Editable arg0) {
    	SharedPreferences prefs = getSharedPreferences("PorraData", 0);

    	Editor edit = prefs.edit();
    	edit.putString("Porra" + codiPorra, arg0.toString());
    	edit.commit();
		
	}

	public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
			int arg3) {
		
	}

	public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
		
	}
}